package com.yanqu.road.dao.impl.activity.advertisement;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.advertisement.AdvertisementSystemData;
import com.yanqu.road.entity.activity.advertisement.AdvertisementUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class AdvertisementActivityUserDataDaoImpl extends TempDataObjectDao<AdvertisementUserData> {
    public static String getAdvertisementUserDataSql = "select * from `t_u_advertisement_activity_user` where `activityId`=? and `userId`=?";
    public AdvertisementUserData getAdvertisementUserData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getAdvertisementUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateAdvertisementUserDataSql =  "update `t_u_advertisement_activity_user` set systemDataMap=?,activeTime=?,totalWatchData=?,earnSpeedBuffRefreshTime=?,earnSpeedBuffCount=?,npcReward=?,npcDailyStage=?,npcLastRefreshTime=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(AdvertisementUserData advertisementUserData) {
        advertisementUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(advertisementUserData.getSystemDataMap())));
        para.put(i++, new DbParameter(Types.BIGINT, advertisementUserData.getActiveTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(advertisementUserData.getTotalWatchData())));

        para.put(i++, new DbParameter(Types.BIGINT, advertisementUserData.getEarnSpeedBuffRefreshTime()));
        para.put(i++, new DbParameter(Types.INTEGER, advertisementUserData.getEarnSpeedBuffCount()));
        para.put(i++, new DbParameter(Types.VARCHAR, advertisementUserData.getNpcReward()));
        para.put(i++, new DbParameter(Types.BIGINT, advertisementUserData.getNpcDailyStage()));
        para.put(i++, new DbParameter(Types.BIGINT, advertisementUserData.getNpcLastRefreshTime()));

        para.put(i++, new DbParameter(Types.INTEGER, advertisementUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, advertisementUserData.getUserId()));
        if (getDb().executeNoneQuery(updateAdvertisementUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertAdvertisementUserDataSql = "insert into `t_u_advertisement_activity_user`(activityId,userId,systemDataMap,activeTime,totalWatchData,earnSpeedBuffRefreshTime,earnSpeedBuffCount,npcReward,npcDailyStage,npcLastRefreshTime) values (?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(AdvertisementUserData advertisementUserData) {
        advertisementUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, advertisementUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, advertisementUserData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(advertisementUserData.getSystemDataMap())));
        para.put(i++, new DbParameter(Types.BIGINT, advertisementUserData.getActiveTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(advertisementUserData.getTotalWatchData())));
        para.put(i++, new DbParameter(Types.BIGINT, advertisementUserData.getEarnSpeedBuffRefreshTime()));

        para.put(i++, new DbParameter(Types.INTEGER, advertisementUserData.getEarnSpeedBuffCount()));
        para.put(i++, new DbParameter(Types.VARCHAR, advertisementUserData.getNpcReward()));
        para.put(i++, new DbParameter(Types.BIGINT, advertisementUserData.getNpcDailyStage()));
        para.put(i++, new DbParameter(Types.BIGINT, advertisementUserData.getNpcLastRefreshTime()));

        if (getDb().executeNoneQuery(insertAdvertisementUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected AdvertisementUserData parseData(ResultSet rs) throws SQLException {
        AdvertisementUserData advertisementUserData = new AdvertisementUserData();
        advertisementUserData.setActivityId(rs.getInt("activityId"));
        advertisementUserData.setUserId(rs.getLong("userId"));
        advertisementUserData.setSystemDataMap(JSON.parseObject(rs.getString("systemDataMap"), new TypeReference<ConcurrentHashMap<Integer, AdvertisementSystemData>>() {}));
        advertisementUserData.setActiveTime(rs.getLong("activeTime"));
        advertisementUserData.setTotalWatchData(JSON.parseObject(rs.getString("totalWatchData"), new TypeReference<ConcurrentHashMap<java.lang.Integer, java.lang.Integer>>() {}));

        advertisementUserData.setEarnSpeedBuffRefreshTime(rs.getLong("earnSpeedBuffRefreshTime"));
        advertisementUserData.setEarnSpeedBuffCount(rs.getInt("earnSpeedBuffCount"));
        advertisementUserData.setNpcReward(rs.getString("npcReward"));
        advertisementUserData.setNpcDailyStage(rs.getLong("npcDailyStage"));
        advertisementUserData.setNpcLastRefreshTime(rs.getLong("npcLastRefreshTime"));

        if (advertisementUserData.getSystemDataMap() == null){
            advertisementUserData.setSystemDataMap(new ConcurrentHashMap<Integer, AdvertisementSystemData>());
        }

        if (advertisementUserData.getTotalWatchData() == null){
            advertisementUserData.setTotalWatchData(new ConcurrentHashMap<Integer, Integer>());
        }
        return advertisementUserData;
    }
}
